package guyubao.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import guyubao.com.GreenLoong.GreenLoong;
import guyubao.com.entity.RestBean;
import guyubao.com.entity.dto.*;
import guyubao.com.entity.vo.request.JDCertificateVoRequest;
import guyubao.com.entity.vo.response.JDAccountInfoVo;
import guyubao.com.entity.vo.response.JDAccountVo;
import guyubao.com.entity.vo.response.JDCertificateVo;
import guyubao.com.service.JDAccountService;
import guyubao.com.service.JDCertificateService;
import guyubao.com.service.LoginMapperUserService;
import guyubao.com.service.RedisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.hibernate.validator.constraints.Length;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@EnableTransactionManagement
@RequestMapping("/api/JDAccount")
@Tag(name = "京东账号相关", description = "账号信息的增删改查")
public class JDAccountController extends BaseController {
    @Resource
    LoginMapperUserService loginMapperUserService;
    @Resource
    JDCertificateService jdCertificateService;
    @Resource
    JDAccountService jdAccountService;
    @Resource
    RedisService redisService;
    @Resource
    GreenLoong greenLoong;
    
    @Operation(summary = "获取用户下所有uid")
    @GetMapping("/uIds")
    public RestBean<List<String>> getUIds(@RequestAttribute String id) {
        return this.innerGetUIds(id);
    }
    
    public RestBean<List<String>> innerGetUIds(String id) {
        return RestBean.success(loginMapperUserService.getUIds(id));
    }
    
    @Operation(summary = "根据uid获取基础 账号 昵称 备注")
    @GetMapping("/account")
    public RestBean<JDAccountDto> getJDAccount(@RequestParam @Length(min = 1, max = 20) String uid, @RequestAttribute String id) {
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) return RestBean.forbidden(verification);
        return this.innerGetJDAccount(uid);
    }
    
    public RestBean<JDAccountDto> innerGetJDAccount(String uid) {
        return RestBean.success(loginMapperUserService.getJDAccount(uid));
    }
    
    @Operation(summary = "获取用户下所有账户 uid pin 昵称 备注 次数 状态 ")
    @GetMapping("/accounts")
    public RestBean<List<JDAccountVo>> getJDAccounts(@RequestAttribute String id) {
        return this.innerGetJDAccounts(id);
    }
    
    public RestBean<List<JDAccountVo>> innerGetJDAccounts(String id) {
        return RestBean.success(loginMapperUserService.getJDAccounts(id));
    }
    
    @Operation(summary = "获取用户下所有账户 uid pin 昵称 备注 次数 状态 ")
    @GetMapping("/accountsPage")
    public RestBean<Page<JDAccountVo>> getJDAccountsPage(@RequestParam Long size,
                                                         @RequestParam Long current,
                                                         @RequestAttribute String id) {
        return this.innerGetJDAccountsPage(id, size, current);
    }
    
    public RestBean<Page<JDAccountVo>> innerGetJDAccountsPage(String id, long size, long current) {
        return RestBean.success(loginMapperUserService.getJDAccountsPage(id, size, current));
    }
    
    @Operation(summary = "根据uid获取该账号所有信息")
    @GetMapping("/accountInfos")
    public RestBean<JDAccountInfoVo> getJDAccountInfos(@RequestParam @Length(min = 1, max = 20) String uid,
                                                       @RequestAttribute String id) {
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) {
            return RestBean.forbidden(verification);
        }
        // 执行业务
        return this.innerGetJDAccountInfos(uid);
    }
    
    public RestBean<JDAccountInfoVo> innerGetJDAccountInfos(String uid) {
        return RestBean.success(loginMapperUserService.getJDAccountInfos(uid));
    }
    
    @Operation(summary = "根据uid获取该账号简略信息")
    @GetMapping("/accountInfo")
    public RestBean<JDAccountInfoDto> getJDAccountInfo(@RequestParam @Length(min = 1, max = 20) String uid,
                                                       @RequestAttribute String id) {
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return this.innerGetJDAccountInfo(uid);
    }
    
    public RestBean<JDAccountInfoDto> innerGetJDAccountInfo(String uid) {
        return RestBean.success(loginMapperUserService.getJDAccountInfo(uid));
    }
    
    
    @Operation(summary = "根据uid删除绑定")
    @DeleteMapping("/account")
    public RestBean<Void> delJDAccount(@RequestParam @Length(min = 1, max = 20) String uid,
                                       @Length(min = 6, max = 6) @RequestParam String code,
                                       @RequestAttribute String id, @RequestAttribute String email) {
        // 验证该账号是否属于用户和验证码
        String verification = verification(uid, id, "relieveJDAccount", email, code);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return this.innerDelJDAccount(uid);
    }
    
    public RestBean<Void> innerDelJDAccount(String uid) {
        return this.messageHandle(() -> loginMapperUserService.delJDAccount(uid));
    }
    
    @Operation(summary = "根据uid更新备注")
    @PutMapping("/comment")
    public RestBean<Void> updateJDAccountComment(@RequestParam @Length(min = 1, max = 20) String uid,
                                                 @RequestParam @Length(min = 1, max = 50) String data,
                                                 @Length(min = 6, max = 6) @RequestParam String code,
                                                 @RequestAttribute String id,
                                                 @RequestAttribute String email) {
        // 验证该账号是否属于用户和验证码
        String verification = verification(uid, id, "updateJDAccountComment", email, code);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return innerUpdateJDAccountComment(uid, data);
    }
    
    public RestBean<Void> innerUpdateJDAccountComment(String uid, String comment) {
        String s = jdAccountService.updateJDAccountComment(uid, comment);
        return s == null ? RestBean.success() : RestBean.failure(s);
    }
    
    @Operation(summary = "根据uid更新京东账号")
    @PutMapping("/account")
    public RestBean<Void> updateJDAccountAccount(@RequestParam @Length(min = 1, max = 20) String uid,
                                                 @RequestParam @Length(min = 1, max = 50) String data,
                                                 @Length(min = 6, max = 6) @RequestParam String code,
                                                 @RequestAttribute String id,
                                                 @RequestAttribute String email) {
        // 验证该账号是否属于用户和验证码
        String verification = verification(uid, id, "updateJDAccountAccount", email, code);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return innerUpdateJDAccountAccount(uid, data);
    }
    
    public RestBean<Void> innerUpdateJDAccountAccount(String uid, String account) {
        String s = jdAccountService.updateJDAccountAccount(uid, account);
        return s == null ? RestBean.success() : RestBean.failure(s);
    }
    
    @Operation(summary = "根据uid更新京东昵称")
    @PutMapping("/nickname")
    public RestBean<Void> updateJDAccountNickname(@RequestParam @Length(min = 1, max = 20) String uid,
                                                  @RequestParam @Length(min = 1, max = 50) String data,
                                                  @Length(min = 6, max = 6) @RequestParam String code,
                                                  @RequestAttribute String id,
                                                  @RequestAttribute String email) {
        // 验证该账号是否属于用户和验证码
        String verification = verification(uid, id, "updateJDAccountNickname", email, code);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return innerUpdateJDAccountNickname(uid, data);
    }
    
    public RestBean<Void> innerUpdateJDAccountNickname(String uid, String nickname) {
        String s = jdAccountService.updateJDAccountNickname(uid, nickname);
        return s == null ? RestBean.success() : RestBean.failure(s);
    }
    
    @Operation(summary = "根据uid获取该账号凭证信息")
    @GetMapping("/proof")
    public RestBean<JDCertificateDto> getJDAccountProof(@RequestParam @Length(min = 1, max = 20) String uid,
                                                        @RequestAttribute String id) {
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return this.innerGetJDAccountProof(uid);
    }
    
    public RestBean<JDCertificateDto> innerGetJDAccountProof(String uid) {
        return RestBean.success(jdCertificateService.getJDCertificateProof(uid));
    }
    
    @Operation(summary = "根据uid获取该账号凭证 次数 状态")
    @GetMapping("/proofBase")
    public RestBean<JDCertificateVo> getJDAccountProofBase(@RequestParam @Length(min = 1, max = 20) String uid,
                                                           @RequestAttribute String id) {
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) return RestBean.forbidden(verification);
        // 执行业务
        return this.innerGetJDAccountProofBase(uid);
    }
    
    public RestBean<JDCertificateVo> innerGetJDAccountProofBase(String uid) {
        return RestBean.success(jdCertificateService.getJDCertificateProofBase(uid));
    }
    
    @Transactional
    @Operation(summary = "更新账号凭证信息")
    @PutMapping("/proof")
    public RestBean<Void> updateJDCertificate(@Parameter(description = "json提供凭证和uid",
            example = "{'pt_key':'xxx','pt_token':'xxx','pt_pin':'xxx','uid':'xxx'}")
                                              @RequestBody @Valid JDCertificateVoRequest vo,
                                              @RequestAttribute String id,
                                              @RequestAttribute String email) {
        String uid = vo.getUid();
        // 验证该账号是否属于用户
        String verification = verification(uid, id);
        if (verification != null) return RestBean.forbidden(verification);
        // 判断验证码
        String redisCode = redisService.verificationCode(vo.getCode(), "updateJDCertificate", email);
        if (redisCode != null) return RestBean.failure(redisCode);
        // 判断是否有条件进行更新
        String s = jdCertificateService.selfAttenuation(uid);
        if (s != null) return RestBean.failure(s);
        // 加锁
        synchronized (this) {
            // 开始将数据推送给青龙
            String login = greenLoong.login();
            if (login != null) return RestBean.failure(login);
            String env = greenLoong.getEnv();
            if (env != null) return RestBean.failure(env);
            String value = "pt_key=" + vo.getPt_key()
                    + ";pt_token=" + vo.getPt_token() + ";pt_pin=" + vo.getPt_pin() + ";";
            String glUpdate = greenLoong.updateEnv(uid, value);
            if (glUpdate != null) return RestBean.failure(glUpdate);
            // 修改账号状态
            String s1 = jdCertificateService.updateJDCertificateState(uid);
            if (s1 != null) throw new RuntimeException(s1);
            // 完成所有业务
            return this.innerUpdateJDCertificate(vo);
        }
    }
    
    public RestBean<Void> innerUpdateJDCertificate(JDCertificateVoRequest vo) {
        return this.messageHandle(vo, jdCertificateService::updateJDCertificate);
    }
}
